
function initYYYY(YYYY,start,end,defaultValue,eleid){
  var y = new Date().getFullYear();
  var hasDefault = true;
  if(!arguments[3] || defaultValue<1 || start>defaultValue){
    hasDefault = false;
    defaultValue = y;
  }
  if(!arguments[1]) start = y-50;
  if(!arguments[2]) end = y+10;
  //先给年下拉框赋内容
  var yearId = YYYY.id;
  var index = yearId.indexOf("-");
  var parentId = yearId.substr(index+1);
  var parent = $(parentId);
  var value = parent.value;
  var year = value.substr(0,4);
  var month = value.substr(5);

  var str = "<option value = '-1" + "' > " + "选择年" + " </option>\r\n";
  for (i = start; i < defaultValue; i++) {
    str += "<option value = '" + i + "' > " + i + "年" + " </option>\r\n";
  }
  if(hasDefault){
     str += "<option value = '" + defaultValue + "' selected='selected'> " + defaultValue + "年" + " </option>\r\n";
     parent.value = defaultValue +"-"+month;
  }else{
     str += "<option value = '" + defaultValue + "' > " + defaultValue + "年" + " </option>\r\n";
  }
  for (i = defaultValue+1; i < end+1; i++) {
    str += "<option value = '" + i + "' > " + i + "年" + " </option>\r\n";
  }

  YYYY.innerHTML = str;
}

function initMM(MM,defaultValue){
  var hasDefault = true;
  if(!arguments[1] || defaultValue<1){
    hasDefault = false;
    defaultValue = 12;
  }
  //赋月份的下拉框
  var monthId = MM.id;
  var index = monthId.indexOf("-");
  var parentId = monthId.substr(index+1);
  var parent = $(parentId);
  var value = parent.value;
  var year = value.substr(0,4);
  var month = value.substr(5);

  var str = "<option value = '-1" + "' > " + "选择月" + " </option>\r\n";
  for (var i = 1; i < defaultValue; i++) {
    str += "<option value = '" + i + "' > " + i + "月" + " </option>\r\n";
  }
  if(hasDefault){
     str += "<option value = '" + defaultValue + "' selected='selected'> " + defaultValue + "月" + " </option>\r\n";
     parent.value = year +"-"+defaultValue;
  }else{
     str += "<option value = '" + defaultValue + "' > " + defaultValue + "月" + " </option>\r\n";
  }
  for (var i = defaultValue+1; i < 13; i++) {
    str += "<option value = '" + i + "' > " + i + "月" + " </option>\r\n";
  }  
  MM.innerHTML = str ;
}
     
     //年发生变化时
  function YYYYMM(eleid,str){
    var index = eleid.indexOf("-");
    var parentId = eleid.substr(index+1);
    var parent = $(parentId);
    var value = parent.value;
    var year = value.substr(0,4);
    var month = value.substr(5);
    parent.value = str +"-"+month;

    var y = new Date().getFullYear();

    if(parentId=="birth"){
      initYYYY($("yyyy-start"),str,y);
      initYYYY($("yyyy-end"),str,y,$("yyyy-end").value);
    }else if(parentId=="start"){
      initYYYY($("yyyy-end"),str,y,$("yyyy-end").value);
    }else{
      console.log(parentId);
    }

/*
    var MMvalue = MM.options[MM.selectedIndex].value;
    if (MMvalue == "") {
      DD.outerHTML = strDD;
      return;
    }
    var n = MonHead[MMvalue - 1];
    if (MMvalue == 2 && IsPinYear(str)) n++;
    writeDay(n)*/
  }

  //月发生变化时
  function MMDD(eleid,str) {
    var index = eleid.indexOf("-");
    var parentId = eleid.substr(index+1);
    var parent = $(parentId);
    var value = parent.value;
    var year = value.substr(0,4);
    var month = value.substr(5);
    parent.value = year +"-"+str;

    console.log(parent.value);
/*    var YYYYvalue = YYYY.options[YYYY.selectedIndex].value;
    if (str == "") {
      DD.outerHTML = strDD;
      return;
    }
    var n = MonHead[str - 1];
    if (str == 2 && IsPinYear(YYYYvalue)) n++;
    writeDay(n)*/
  }
   //据条件写日期的下拉框
	function writeDay(n){
		var s = strDD.substring(0, strDD.length - 9);
		for (var i = 1; i < (n + 1); i++) s += " < option value = '" + i + "' > " + i + "日" + " < /option>\r\n";
		DD.outerHTML = s + "</select > ";
	}

	//判断是否闰平年
	function IsPinYear(year){
		return (0 == year % 4 && (year % 100 != 0 || year % 400 == 0))
	}

	function $(id){
	   return document.getElementById(id);
	}

	function show(element){
	  element.style.display = "block";
	}
	            
	function hide(element){
	  element.style.display = "none";
	}

  function test() {
  
  YYYY = $("yyyy");
    MM = $("mm");
    DD = $("dd");

    strYYYY = $("yyyy").outerHTML;
    strMM = $("mm").outerHTML;
    strDD = $("dd").outerHTML;
    MonHead = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

    //先给年下拉框赋内容
    var y = new Date().getFullYear();
    var str = strYYYY.substring(0, strYYYY.length - 9);

    for (var i = (y - 50); i < (y + 10); i++) //以今年为准，前30年，后30年
    {
      str += "<option value = '" + i + "' > " + i + "年" + " </option>\r\n";
    }
    YYYY.outerHTML = str + "</select > ";

    //赋月份的下拉框
    var str = strMM.substring(0, strMM.length - 9);
    for (var i = 1; i < 13; i++) {
      str += "<option value = '" + i + "' > " + i + "月" + " </option>\r\n";
    }
    MM.outerHTML = str + "</select > ";

    YYYY.value = y;
    MM.value = new Date().getMonth() + 1;
    var n = MonHead[new Date().getMonth()];
    if (new Date().getMonth() == 1 && IsPinYear(YYYYvalue)) n++;
    writeDay(n); //赋日期下拉框
    DD.value = new Date().getDate();
}